﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace EndlessGalaxy
{
    public partial class Form1 : Form
    {
        int currentsystem;
        int tickCount = 0;
        string clickmode;
        Point menucenter;
        int selectedship;
        List<Point> menuitemlocations;//CW from top

        public Form1()
        {
            InitializeComponent();
            this.CenterToScreen();
            menuitemlocations = new List<Point>() { new Point(-25, -85), new Point(20, -75), new Point(35, -25), 
                new Point(21, 21), new Point(-25, 35), new Point(-69, 20), new Point(-87, -29), new Point(-71, -73) };
            new Globals(this);
            clickmode = "select";
            Skybox home = new Skybox();
            while (home.HasBHole() || !home.HasHabitable(new List<string>() { "STRUCTURAL", "SEMICONDUCTOR", "CONDUCTOR" }))
            {
                Globals.usednames.Remove(home.name);
                Globals.unusednames.Add(home.name);
                home = new Skybox();
            }
            int habitable = home.HabitableWorld(new List<string>() { "STRUCTURAL", "SEMICONDUCTOR", "CONDUCTOR" });
            string data = "Name: " + home.name + "\nStar 1 size: " + home.planets[0].radius;
            if (home.suns > 1)
            {
                data += "\nStar 2 size: " + home.planets[1].radius;
                if (home.suns > 2)
                {
                    data += "\nStar 3 size: " + home.planets[2].radius;
                }
            }
            data += "\nPlanets: " + home.mplanets;
            label1.Text = data;
            home.planets[habitable].AddColony(new List<Facility>());
            home.planets[habitable].facilities.Add(new Facility(1, new List<Cargo>() {
                    new Cargo("Water", 0.1, Cargo.Phases.liquid) }, new List<Cargo>() { 
                        new Cargo("Hydrogen", (0.2/ 3.0), Cargo.Phases.gas), new Cargo("Oxygen", (0.1 / 3.0), Cargo.Phases.gas) }));
            home.planets[habitable].facilities.Add(new Facility(1, null, 'm'));
            while (!Cargo.hasAttribute(home.planets[habitable].surface, "LIFE_COMMON") && 
                !Cargo.hasAttribute(home.planets[habitable].surface, "LIFE_UNCOMMON") &&
                !Cargo.hasAttribute(home.planets[habitable].surface, "LIFE_RARE"))
            {
                switch (Globals.randint.Next(6))
                {
                    case 0:
                    case 1:
                    case 2:
                        home.planets[habitable].surface.Add(SuperCargo.GiveWithAttribute("LIFE_COMMON", 
                            Globals.randint.NextDouble() * Math.Pow(34000.0, Globals.randint.NextDouble() * 3)));
                        break;
                    case 3:
                    case 4:
                        home.planets[habitable].surface.Add(SuperCargo.GiveWithAttribute("LIFE_UNCOMMON",
                            Globals.randint.NextDouble() * Math.Pow(34000.0, Globals.randint.NextDouble() * 3)));
                        break;
                    case 5:
                        home.planets[habitable].surface.Add(SuperCargo.GiveWithAttribute("LIFE_RARE",
                            Globals.randint.NextDouble() * Math.Pow(34000.0, Globals.randint.NextDouble() * 3)));
                        break;
                    default:
                        goto case 0;
                }
            }
            for (int i = 0; i < home.planets[habitable].facilities.Count; i++)
            {
                home.planets[habitable].facilities[i].location = habitable;
            }
            //Test ships
            Ship.PopulateTemplate("SandboxShip", 10, 1, 1, true, true, 1000);
            Ship.PopulateTemplate("SandboxShip2", 10, 1, 2, true, true, 10000);
            for (int i = 0; i < 6; i++)
            {
                home.ships.Add(new Ship("SandboxShip2", home.planets[habitable]));
                home.ships[i].orbitedindex = habitable;
            }
            home.DrawShips();
            //Test ships end
            MainView.BackgroundImage = home.displayed;
            Globals.knownsystems.Add(home);
            MainTimer.Enabled = true;
        }

        private void MainView_MouseClick(object sender, MouseEventArgs e)
        {
            int X = e.Location.X;
            int Y = e.Location.Y;
            string data = "";
            switch (clickmode)
            {
                case "select":
                    for (int i = 0; i < Globals.knownsystems[currentsystem].planets.Count; i++)
                    {
                        if (Math.Sqrt(Math.Pow((Globals.knownsystems[currentsystem].planets[i].location.X - X), 2) + Math.Pow((Globals.knownsystems[currentsystem].planets[i].location.Y - Y), 2)) <= Globals.knownsystems[currentsystem].planets[i].imgsize / 2.1)
                        {
                            if (Globals.knownsystems[currentsystem].Orbits(i).Count > 0)// spawn ComboBox to select
                            {
                                clickmode = "disabledSelect"; //combo box will change it to menu
                                MainTimer.Enabled = false; //this stays here.
                                comboBox1.Location = new Point(Math.Max(0, e.Location.X - comboBox1.Size.Width), e.Location.Y);
                                comboBox1.Items.Clear();
                                comboBox1.Items.Add(Globals.knownsystems[currentsystem].planets[i]);
                                for (int j = 0; j < Globals.knownsystems[currentsystem].ships.Count; j++)
                                {
                                    if (Globals.knownsystems[currentsystem].Orbits(i).Contains(Globals.knownsystems[currentsystem].ships[j]))
                                    {
                                        Globals.knownsystems[currentsystem].ships[j].index = j;
                                        comboBox1.Items.Add(Globals.knownsystems[currentsystem].ships[j]);
                                    }
                                }
                                comboBox1.Tag = i;
                                comboBox1.Visible = true;
                                break;
                            }
                            //selected object referencer goes here
                            DataView viewer = new DataView(Globals.knownsystems[currentsystem].planets[i]);
                            viewer.Show();
                        }
                    }
                    for (int i = 0; i < Globals.knownsystems[currentsystem].ships.Count; i++)
                    {
                        if (Globals.knownsystems[currentsystem].ships[i].orbitedindex < 0)
                        {
                            if (Math.Sqrt(Math.Pow((Globals.knownsystems[currentsystem].ships[i].location.X - X), 2) +
                                Math.Pow((Globals.knownsystems[currentsystem].ships[i].location.Y - Y), 2)) <= 3)
                            {
                                MainTimer.Enabled = false;
                                Graphics canvas = Graphics.FromImage(MainView.BackgroundImage);
                                Rectangle location = new Rectangle(Math.Max(0, Math.Min(400,
                                    Globals.knownsystems[currentsystem].ships[i].location.X - 100)),
                                    Math.Max(0, Math.Min(400,
                                    Globals.knownsystems[currentsystem].ships[i].location.Y - 100)), 200, 200);
                                canvas.DrawImage(Image.FromFile("piemenu.png"), location);
                                menucenter = new Point(location.Left + 100, location.Top + 100);
                                selectedship = i;
                                clickmode = "menu";
                                Refresh();
                                comboBox1.Visible = false;
                                break;
                            }
                        }
                    }
                    break;
                case "menu":
                    for (int i = 0; i < 8; i++)
                    {
                        if (new Rectangle(menucenter.X + menuitemlocations[i].X, menucenter.Y + menuitemlocations[i].Y, 48, 48).
                            Contains(e.Location))
                        {
                            switch (i)
                            {
                                case 0://N, "Move"
                                    clickmode = "move";
                                    Globals.knownsystems[currentsystem].Cleanup();
                                    Globals.knownsystems[currentsystem].Drawplanets();
                                    Globals.knownsystems[currentsystem].DrawShips();
                                    MainView.BackgroundImage = Globals.knownsystems[currentsystem].displayed;
                                    MainView.Refresh();
                                    data = "Select location to move the ship to.";
                                    break;
                                case 1://NE, "Take Cargo"
                                    if (Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex >= 0)
                                    {
                                        comboBox1.Location = new Point(Math.Max(0, e.Location.X - comboBox1.Size.Width), e.Location.Y);
                                        comboBox1.Items.Clear();
                                        comboBox1.Items.Add("Never mind");
                                        foreach (Cargo item in Globals.knownsystems[currentsystem].planets[Globals.
                                            knownsystems[currentsystem].ships[selectedship].orbitedindex].surface)
                                        {
                                            comboBox1.Items.Add(item);
                                        }
                                        comboBox1.Visible = true;
                                        clickmode = "takeCargo";
                                    }
                                    else
                                    {
                                        clickmode = "select";
                                        MainTimer.Enabled = true;
                                    }
                                    break;
                                case 2://E, "Give Cargo"
                                    if (Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex >= 0)
                                    {
                                        comboBox1.Location = new Point(Math.Max(0, e.Location.X - comboBox1.Size.Width), e.Location.Y);
                                        comboBox1.Items.Clear();
                                        comboBox1.Items.Add("Never mind");
                                        foreach (Cargo item in Globals.knownsystems[currentsystem].ships[selectedship].cargo)
                                        {
                                            comboBox1.Items.Add(item);
                                        }
                                        comboBox1.Visible = true;
                                        clickmode = "giveCargo";
                                    }
                                    else
                                    {
                                        clickmode = "select";
                                        MainTimer.Enabled = true;
                                    }
                                    break;
                                case 3://SE
                                    clickmode = "select";//Empty button for now
                                    MainTimer.Enabled = true;
                                    break;
                                case 4://S, "Other" (Spawn another menu for further items, when empty buttons filled)
                                    clickmode = "select";//Empty button for now
                                    MainTimer.Enabled = true;
                                    break;
                                case 5://SW, "Build"
                                    comboBox1.Location = new Point(Math.Max(0, e.Location.X - comboBox1.Size.Width), e.Location.Y);
                                    comboBox1.Items.Clear();
                                    comboBox1.Items.Add("Never mind");
                                    foreach (Cargo item in Globals.knownsystems[currentsystem].ships[selectedship].cargo)
                                    {
                                        comboBox1.Items.Add(item);
                                    }
                                    comboBox1.Visible = true;
                                    clickmode = "buildPlanet";
                                    break;
                                case 6://W, "Mine"
                                    if (Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex >= 0)
                                    {
                                        switch (Globals.knownsystems[currentsystem].ships[selectedship].mineLevel)
                                        {
                                            case 1:
                                                Globals.knownsystems[currentsystem].ships[selectedship].action = new activity("mine",
                                                   Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].
                                                   ships[selectedship].orbitedindex]);
                                                comboBox1.Visible = false;
                                                clickmode = "select";
                                                MainTimer.Enabled = true;
                                                break;
                                            case 2:
                                                clickmode = "minechoice";
                                                if (Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex >= 0)
                                                {
                                                    comboBox1.Location = new Point(Math.Max(0, e.Location.X - comboBox1.Size.Width), e.Location.Y);
                                                    comboBox1.Items.Clear();
                                                    comboBox1.Items.Add("Never mind");
                                                    comboBox1.Items.Add("Quarry");
                                                    foreach (Cargo item in Globals.knownsystems[currentsystem].planets[Globals.
                                                        knownsystems[currentsystem].ships[selectedship].orbitedindex].composition)
                                                    {
                                                        comboBox1.Items.Add(item.name);
                                                    }
                                                    comboBox1.Visible = true;
                                                }
                                                else
                                                {
                                                    clickmode = "select";
                                                    MainTimer.Enabled = true;
                                                }
                                                break;
                                            default:
                                                clickmode = "select";
                                                MainTimer.Enabled = true;
                                                break;
                                        }
                                    }
                                    else
                                    {
                                        clickmode = "select";
                                        MainTimer.Enabled = true;
                                    }
                                    break;
                                case 7://NW, blank
                                    clickmode = "select";//Empty button for now
                                    MainTimer.Enabled = true;
                                    break;
                                default:
                                    clickmode = "select";//Empty button for now
                                    MainTimer.Enabled = true;
                                    break;
                            }
                        }
                    }
                    if (clickmode == "menu")
                    {
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    break;
                case "move":
                    Globals.knownsystems[currentsystem].ships[selectedship].destination = e.Location;
                    Globals.knownsystems[currentsystem].ships[selectedship].destindex = -1;
                    for (int i = 0; i < Globals.knownsystems[currentsystem].planets.Count; i++)
                    {
                        if (Math.Sqrt(Math.Pow((Globals.knownsystems[currentsystem].planets[i].location.X - X), 2) +
                            Math.Pow((Globals.knownsystems[currentsystem].planets[i].location.Y - Y), 2)) <=
                            Globals.knownsystems[currentsystem].planets[i].imgsize / 2)
                        {
                            Globals.knownsystems[currentsystem].ships[selectedship].destindex = i;
                            Globals.knownsystems[currentsystem].ships[selectedship].destination =
                                Globals.knownsystems[currentsystem].planets[i].location;
                        }
                    }
                    clickmode = "select";
                    MainTimer.Enabled = true;
                    break;
            }
            label1.Text = data;
        }

        private void MainTimer_Tick(object sender, EventArgs e)
        {
            currentsystem = Globals.knownsystems.Count - 1;
            Globals.currentsystem = currentsystem;
            tickCount++;
            tickCount %= 5;
            #region black hole
            if (Globals.knownsystems[currentsystem].HasBHole())
            {
                Point BHoleLocation = new Point(300, 300);
                int hole = 0;
                for (int i = 0; i < Globals.knownsystems[currentsystem].planets.Count; i++)
                {
                    if (Globals.knownsystems[currentsystem].planets[i].type == Planet.worldType.bHole)
                    {
                        BHoleLocation = Globals.knownsystems[currentsystem].planets[i].location;
                        hole = i;
                    }
                }
                for (int i = 0; i < Globals.knownsystems[currentsystem].planets.Count; i++)
                {
                    if (Globals.knownsystems[currentsystem].planets[i].type != Planet.worldType.bHole)
                    {
                        Point distance = new Point((Globals.knownsystems[currentsystem].planets[i].location.X - BHoleLocation.X),
                                (Globals.knownsystems[currentsystem].planets[i].location.Y - BHoleLocation.Y));
                        if (Globals.knownsystems[currentsystem].planets[i].type == Planet.worldType.wisp)
                        {
                            if ((distance.X == 0) || (distance.Y == 0))
                            {
                                Globals.knownsystems[currentsystem].planets[hole].composition[0].amount +=
                                   Globals.knownsystems[currentsystem].planets[i].composition[0].amount;
                                Globals.knownsystems[currentsystem].planets.RemoveAt(i);
                                continue;
                            }
                            if (Math.Sqrt((distance.X * distance.X) + (distance.Y * distance.Y)) <= (1 / ((distance.X * distance.X) + (distance.Y * distance.Y))))
                            {
                                Globals.knownsystems[currentsystem].planets[hole].composition[0].amount +=
                                    Globals.knownsystems[currentsystem].planets[i].composition[0].amount;
                                Globals.knownsystems[currentsystem].planets.RemoveAt(i);
                            }
                            else
                            {
                                Globals.knownsystems[currentsystem].planets[i].location =
                                    Globals.MovePointTowards(Globals.knownsystems[currentsystem].planets[i].location, BHoleLocation,
                                    Math.Max(Convert.ToInt32(1 / ((distance.X * distance.X) + (distance.Y * distance.Y))), 1));
                            }
                        }
                        else
                        {
                            if (tickCount == 0)
                            {
                                Globals.knownsystems[currentsystem].planets.Add(new Planet(Planet.worldType.wisp,
                                    Globals.knownsystems[currentsystem].planets[i].GiveRandomCargo(Math.Round(10000 / Math.Sqrt((distance.X * distance.X)
                                    + (distance.Y * distance.Y)), 0)), new System.Drawing.Point(
                                        Globals.knownsystems[currentsystem].planets[i].location.X + Globals.CenterRectangle(1,
                                        Globals.knownsystems[currentsystem].planets[i].imgsize),
                                        Globals.knownsystems[currentsystem].planets[i].location.Y + Globals.CenterRectangle(1,
                                        Globals.knownsystems[currentsystem].planets[i].imgsize))));
                            }
                        }
                    }
                }
            }
            #endregion
            foreach (Ship that in Globals.knownsystems[currentsystem].ships)
            {
                if (that.location != that.destination)
                {
                    that.orbitedindex = -1;
                    that.action = new activity("moving");
                    if (Globals.Distance(that.location, that.destination) < (that.speed / 5))//Changed because I wanted to see how well it worked at 10 FPS instead of 2.
                    {
                        that.location = that.destination;
                        if (that.destindex > -1)
                        {
                            that.orbitedindex = that.destindex;
                        }
                        that.action = new activity();
                    }
                    else
                    {
                        that.location = Globals.MovePointTowards(that.location, that.destination, that.speed / 5);//Also changed cause of above reason on 310
                    }
                }
                else
                {
                    switch (that.action.action)
                    {
                        case "takeCargo":
                            Cargo.GiveCargo(Cargo.TakeCargo(that.action.cargo.name,
                                Math.Min(Cargo.FreeSpace(that.cargosize, that.cargo), 1),
                                that.action.ptarget.surface), that.cargo);
                            if (!Cargo.HasContents(that.action.cargo.name, that.action.ptarget.surface))
                            {
                                that.action = new activity();
                            }
                            break;
                        case "giveCargo":
                            Cargo.GiveCargo(Cargo.TakeCargo(that.action.cargo.name, 1, that.cargo), that.action.ptarget.surface);
                            if (!Cargo.HasContents(that.action.cargo.name, that.cargo))
                            {
                                that.action = new activity();
                            }
                            break;
                        case "buildPlanet":
                            Cargo.GiveCargo(Cargo.TakeCargo(that.action.cargo.name, 10.0, that.cargo), that.action.ptarget.composition);
                            if (!Cargo.HasContents(that.action.cargo.name, that.cargo))
                            {
                                that.action = new activity();
                            }
                            break;
                        case "mine":
                            if (that.action.cargo != null)
                            {
                                Cargo.GiveCargo(Cargo.TakeCargo(that.action.cargo.name,
                                    Math.Min(Cargo.FreeSpace(that.cargosize, that.cargo), 10.0),
                                    that.action.ptarget.composition), that.cargo);
                                if (!Cargo.HasContents(that.action.cargo.name, that.action.ptarget.composition))
                                {
                                    if (Cargo.Sum(that.action.ptarget.composition) <= 0)
                                    {
                                        that.orbitedindex = -1;
                                    }
                                    that.action = new activity();
                                }
                            }
                            else
                            {
                                List<Cargo> stuffs = that.action.ptarget.CompositionTop();
                                double index = Globals.randint.NextDouble() * Cargo.Sum(stuffs);
                                for (int i = 0; i < stuffs.Count; i++)
                                {
                                    if (i == 0)
                                    {
                                        if (index <= Cargo.Sum(stuffs.GetRange(0, 1)))
                                        {
                                            index = i;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if ((index >= Cargo.Sum(stuffs.GetRange(0, i))) && (index <= Cargo.Sum(stuffs.GetRange(0, i + 1))))
                                        {
                                            index = i;
                                            break;
                                        }
                                    }
                                }
                                Cargo.GiveCargo(Cargo.TakeCargo(that.action.ptarget.composition[Convert.ToInt32(index)].name,
                                    Math.Min(Cargo.FreeSpace(that.cargosize, that.cargo), 1),
                                    that.action.ptarget.composition), that.cargo);
                                if (Cargo.Sum(that.action.ptarget.composition) <= 0)
                                {
                                    that.orbitedindex = -1;
                                    that.action = new activity();
                                }
                            }
                            break;
                    }
                }
            }
            foreach (Planet that in Globals.knownsystems[currentsystem].planets)
            {
                if (that.hasColony)
                {
                    foreach (Facility there in that.facilities)
                    {
                        bool allcontents = true;
                        if (there.miner)
                        {
                            if (there.output != null)
                            {
                                foreach (Cargo item in there.input)
                                {
                                    if (!Cargo.HasContents(item.name, that.composition, item.amount))
                                    {
                                        allcontents = false;
                                        break;
                                    }
                                    if (Math.Sqrt(4 * Math.PI * Math.Pow((that.Gravity() / ((4.0 / 3.0) * Math.PI)), (2.0 / 3.0))) <= that.facilities.Count * 100)
                                    {
                                        allcontents = false;
                                        break;
                                    }
                                }
                                if (allcontents)
                                {
                                    foreach (Cargo item in there.input)
                                    {
                                        Cargo.TakeCargo(item.name, item.amount, that.composition);
                                    }
                                    foreach (Cargo item in there.output)
                                    {
                                        Cargo.GiveCargo(Globals.Copy(item), that.surface);
                                    }
                                }
                            }
                            else
                            {
                                if (Math.Sqrt(4 * Math.PI * Math.Pow((that.Gravity() / ((4.0 / 3.0) * Math.PI)), (2.0 / 3.0))) <= that.facilities.Count * 100)
                                {
                                    allcontents = false;
                                    break;
                                }
                                if (allcontents)
                                {
                                    List<Cargo> stuffs = that.CompositionTop();
                                    double index = Globals.randint.NextDouble() * Cargo.Sum(stuffs);
                                    for (int i = 0; i < stuffs.Count; i++)
                                    {
                                        if (i == 0)
                                        {
                                            if (index <= Cargo.Sum(stuffs.GetRange(0, 1)))
                                            {
                                                index = i;
                                                break;
                                            }
                                        }
                                        else
                                        {
                                            if ((index >= Cargo.Sum(stuffs.GetRange(0, i))) && (index <= Cargo.Sum(stuffs.GetRange(0, i + 1))))
                                            {
                                                index = i;
                                                break;
                                            }
                                        }
                                    }
                                    Cargo.GiveCargo(Cargo.TakeCargo(that.composition[Convert.ToInt32(index)].name, 1.0, that.composition),
                                        that.surface);
                                }
                            }
                        }
                        else
                        {
                            foreach (Cargo item in there.input)
                            {
                                if (!Cargo.HasContents(item.name, that.surface, item.amount))
                                {
                                    allcontents = false;
                                    break;
                                }
                            }
                            if (allcontents)
                            {
                                foreach (Cargo item in there.input)
                                {
                                    Cargo.TakeCargo(item.name, item.amount, that.surface);
                                }
                                foreach (Cargo item in there.output)
                                {
                                    Cargo.GiveCargo(Globals.Copy(item), that.surface);
                                }
                            }
                        }
                    }
                }
            }
            label3.Text = Globals.getData(Globals.knownsystems[Globals.currentsystem].planets[Globals.knownsystems[Globals.currentsystem].suns + 1]);
            Globals.knownsystems[currentsystem].Cleanup();
            Globals.knownsystems[currentsystem].Drawplanets();
            Globals.knownsystems[currentsystem].DrawShips();
            string Text = "";
            foreach (Ship that in Globals.knownsystems[currentsystem].ships)
            {
                Text += that + "\n";
            }
            label2.Text = Text;
            MainView.BackgroundImage = Globals.knownsystems[currentsystem].displayed;
            MainView.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MainTimer.Enabled = false;
            Skybox home = new Skybox();
            string data = "Name: " + home.name + "\nStar 1 size: " + home.planets[0].radius;
            if (home.suns > 1)
            {
                data += "\nStar 2 size: " + home.planets[1].radius;
                if (home.suns > 2)
                {
                    data += "\nStar 3 size: " + home.planets[2].radius;
                }
            }
            data += "\nPlanets: " + home.mplanets;
            label1.Text = data;
            MainView.BackgroundImage = home.displayed;
            Globals.knownsystems.Add(home);
            MainTimer.Enabled = true;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (clickmode)
            {
                case "disabledSelect":
                    if (comboBox1.SelectedIndex == 0)
                    {
                        int i = Convert.ToInt32(comboBox1.Tag);
                        DataView viewer = new DataView(Globals.knownsystems[currentsystem].planets[i]);
                        viewer.Show();
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    if (comboBox1.SelectedIndex > 0)
                    {
                        int i = Convert.ToInt32(comboBox1.Tag);
                        Graphics canvas = Graphics.FromImage(MainView.BackgroundImage);
                        selectedship = (comboBox1.SelectedItem as Ship).index;
                        Rectangle location = new Rectangle(Math.Max(0, Math.Min(400,
                            Globals.knownsystems[currentsystem].Orbits(i)[comboBox1.SelectedIndex - 1].location.X - 100)),
                            Math.Max(0, Math.Min(400,
                            Globals.knownsystems[currentsystem].Orbits(i)[comboBox1.SelectedIndex - 1].location.Y - 100)), 200, 200);
                        menucenter = new Point(location.Left + 100, location.Top + 100);
                        canvas.DrawImage(Image.FromFile("piemenu.png"), location);
                        comboBox1.Visible = false;
                        clickmode = "menu";
                        Refresh();
                    }
                    break;
                case "takeCargo":
                    if (comboBox1.SelectedIndex == 0)
                    {
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    if (comboBox1.SelectedIndex > 0)
                    {
                        Globals.knownsystems[currentsystem].ships[selectedship].action = new activity("takeCargo",
                            Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex]
                            .surface[comboBox1.SelectedIndex - 1],
                           Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex]);
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    break;
                case "giveCargo":
                    if (comboBox1.SelectedIndex == 0)
                    {
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    if (comboBox1.SelectedIndex > 0)
                    {
                        Globals.knownsystems[currentsystem].ships[selectedship].action = new activity("giveCargo",
                            Globals.knownsystems[currentsystem].ships[selectedship].cargo[comboBox1.SelectedIndex - 1],
                           Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex]);
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    break;
                case "buildPlanet":
                    if (comboBox1.SelectedIndex == 0)
                    {
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    if (comboBox1.SelectedIndex > 0)
                    {
                        if (Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex == -1)
                        {
                            Globals.knownsystems[currentsystem].planets.Add(new Planet(Planet.worldType.rockyWorld, new Cargo(),
                                Globals.knownsystems[currentsystem].ships[selectedship].location));
                            Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex =
                                Globals.knownsystems[currentsystem].planets.Count - 1;
                        }
                        Globals.knownsystems[currentsystem].ships[selectedship].action = new activity("buildPlanet",
                            Globals.knownsystems[currentsystem].ships[selectedship].cargo[comboBox1.SelectedIndex - 1],
                           Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].
                           ships[selectedship].orbitedindex]);
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    break;
                case "minechoice":
                    if (comboBox1.SelectedIndex == 0)
                    {
                        comboBox1.Visible = false;
                        clickmode = "select";
                        MainTimer.Enabled = true;
                    }
                    if (comboBox1.SelectedIndex == 1)
                    {
                        Globals.knownsystems[currentsystem].ships[selectedship].action = new activity("mine",
                            Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex]);
                    }
                    if (comboBox1.SelectedIndex > 0)
                    {
                        Globals.knownsystems[currentsystem].ships[selectedship].action = new activity("mine",
                            Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex].
                            composition[comboBox1.SelectedIndex - 2],
                            Globals.knownsystems[currentsystem].planets[Globals.knownsystems[currentsystem].ships[selectedship].orbitedindex]);
                    }
                    comboBox1.Visible = false;
                    clickmode = "select";
                    MainTimer.Enabled = true;
                    break;
            }
            this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            comboBox1.SelectedIndex = -1;
            comboBox1.Text = "Select object";
            comboBox1.ResetText();
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
        }

        public void pauseTimer()
        {
            MainTimer.Enabled = false;
        }

        public void startTimer()
        {
            MainTimer.Enabled = true;
        }
    }
}